﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Dzzzr;

namespace NWS
{
  public static class Helper
  {
    /// <summary>
    /// Current user name
    /// </summary>
    public static string UserName
    {
      get
      {
        return (string) HttpContext.Current.Session[Constants.CurrentUser];
      }
    }

    /// <summary>
    /// Curremt user password (hashed)
    /// </summary>
    public static string Password
    {
      get
      {
        return (string)HttpContext.Current.Session[Constants.CurrentPassword];
      }
    }

    /// <summary>
    /// TeamID
    /// </summary>
    public static int TeamID
    {
      get
      {
        return (int) HttpContext.Current.Profile["TeamID"];
        //if (HttpContext.Current.Session["TeamID"] == null)
        //  HttpContext.Current.Session["TeamID"] = HttpContext.Current.Profile["TeamID"];
        //return (int)HttpContext.Current.Session[Constants.TeamID];
      }
    }


	//public static string CreateInfoHtml(Marker marker, Entity rootEntity, Entity[] subEntities)
	//{
	//  string sourcePath = HttpContext.Current.Server.MapPath("App_Data//MarkerInfo.html");
	//  TemplateManager mngr = TemplateManager.FromFile(sourcePath);
	//  mngr.SetValue("marker", marker);
	//  mngr.SetValue("entities", subEntities);
	//  string source = File.ReadAllText(sourcePath);
	//  string markerInfo = GetMarkerInfo(marker);
	//  source = source.Replace("<MarkerInfo/>", markerInfo);
	//  return source;
	//}

	//private static string GetMarkerInfo(Marker marker)
	//{
	//  return "Name; Coordinates; Speed; Last update";
	//}

    public static SqlConnection Connection
    {
      get
      {
        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString);
        cnn.Open();
        return cnn;
      }
    }

    public static Game CurrentGame
    {
      get { return (Game) HttpContext.Current.Session[Constants.CurrentGame]; }
    }

    public static void UpdateGame(int gameID)
    {
      HttpContext.Current.Session[Constants.CurrentGame] = Game.GetGameByID(gameID);
    }
  }
}
